package com.efast.dmom.aps.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.bean.Workshop;
import com.efast.dmom.aps.entity.base.IModificationFlags;
import com.efast.dmom.aps.service.WorkShopService;

/**
 * 
 * @author wj
 * @date mofify 2020/2/24
 *
 */
@Controller
@RequestMapping("/workShop")
public class WorkShopController {
	@Autowired
	private WorkShopService workShopService;

	@RequestMapping(value = "/operateByWorkshop")
	public @ResponseBody ResultBean operateByWorkshop(@RequestBody Workshop workShop) {
		ResultBean resultBean = new ResultBean(true, ' ');
		
		// Determine the new
		if (workShop.getInnerData().getModificationFlag() == IModificationFlags.NEW) {
			resultBean = workShopService.save(workShop);
			// Judge update
		} else if (workShop.getInnerData().getModificationFlag() == IModificationFlags.MODIFIED) {
			resultBean = workShopService.update(workShop);
			// Judge to delete
		} else if (workShop.getInnerData().getModificationFlag() == IModificationFlags.DELETED) {
			resultBean = workShopService.delete(workShop);
		}
		return resultBean;
	}

	// get workShop by id
	@RequestMapping(value = "/getWorkshopById/{id}")
	public @ResponseBody Workshop getWorkshopById(@PathVariable long id) {
		return workShopService.getById(id);
	}

	// get workShop by name
	@RequestMapping(value = "/getWorkshopByName/{name}")
	public @ResponseBody Workshop getWorkshopByName(@PathVariable String name) {
		return workShopService.getByName(name);
	}

	// get workShop by code
	@RequestMapping(value = "/getWorkshopByCode/{plantCode}")
	public @ResponseBody Workshop getWorkshopByCode(@PathVariable String workshopCode) {
		return workShopService.getByCode(workshopCode);
	}
	
	//get workShop by plantId
	@RequestMapping(value = "/getWorkshopByPlantId/{plantId}")
	public @ResponseBody List<Workshop> getWorkshopByPlantId(@PathVariable long plantId) {
		return workShopService.getWorkshopByPlantId(plantId);
	}
	
	@RequestMapping(value = "/getWorkshopCodeIdNameByPlantId/{plantId}")
	public @ResponseBody ResultBean getWorkshopCodeIdNameByPlantId(@PathVariable long plantId) {
		ResultBean resultBean = new ResultBean(true);
		resultBean.setObject(workShopService.getWorkshopCodeIdNameByPlantId(plantId));
		return resultBean;
	}
}
